<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    @close="closeDialog()"
    :visible.sync="visible"
    width="550px">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="100px">
      <el-form-item label="id" prop="id" hidden>
        <el-input v-model="dataForm.id" placeholder="id" hidden ></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <el-form-item label="主机" prop="host">
            <el-input v-model="dataForm.host" placeholder="主机"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="端口" prop="port">
            <el-input v-model="dataForm.port" placeholder="端口"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="netty端口" prop="nettyPort">
            <el-input v-model="dataForm.nettyPort" placeholder="netty端口"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>
<style>
  .tip {
    padding: 8px 16px;
    background-color: #ecf8ff;
    border-radius: 4px;
    border-left: 5px solid #50bfff;
    margin: 20px 0;
  }
</style>
<script>
  import {getObj, addObj, putObj} from '@/api/job/dmz'
  export default {
    created() {
    },
    data() {
      return {
        visible: false,
        canSubmit: false,

        dataForm: {
          id : '',
          host: '',
          port: '',
          nettyPort: ''

        },
        dataRule: {

        }
      }
    },
    methods: {
      init(id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          console.log(this.dataForm)
          if (id) {
            getObj(id).then(response => {
              this.dataForm = response.data.data;
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
              putObj(this.dataForm).then(data => {
                this.$notify.success('修改成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            } else {
              addObj(this.dataForm).then(data => {
                this.$notify.success('添加成功')
                this.visible = false
                this.$emit('refreshDataList')
              }).catch(() => {
                this.canSubmit = true;
              });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
        this.$refs["dataForm"].resetFields()
      },
    }
  }
</script>

